package ar.edu.itba.cg.score;

import java.util.ArrayList;
import java.util.List;

import ar.edu.itba.cg.util.TextureUtil;

import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.Text;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;

public class BoardScore {
	
	private Quad board;
	private Quad number;
	private DisplaySystem display;
	
	private Vector3f position= new Vector3f(400,550,0);
	private final static int FIRST_ROW=22;
	private final static int SECOND_ROW=-28;
	private final static int TOTAL_ROW=20;

	private final static int CELL=20;
	private final static int SPACE=12;
	
	private final static int WIDHT=300;
	private final static int HIGHT=100;
	
	private List<PointBoardScore> scores;
	
	
	public BoardScore(DisplaySystem display, int player){
		this.display= display;
		
		board= new Quad("Board Score", WIDHT, HIGHT);
		number = new Quad("Number", 30, HIGHT);		
		
		board.setRenderQueueMode(Renderer.QUEUE_ORTHO);
		TextureState ts=TextureUtil.getTexture(display, "board_score_player" + player + ".jpg");
		board.setRenderState(ts);
		
		System.out.println(position);
		board.setLocalTranslation(position);
		
		scores= new ArrayList<PointBoardScore>();		
	}
	
	public void show(){
		board.updateRenderState();
		board.draw(display.getRenderer());
		
		number.updateRenderState();
		number.draw(display.getRenderer());
		
		drawScores();
	}
	
	public void changePosition(int pos){
		position= new Vector3f(position.getX(), 550-100*pos, position.getZ());
		board.setLocalTranslation(position);
		
		number.setRenderQueueMode(Renderer.QUEUE_ORTHO);
		TextureState ts1=TextureUtil.getTexture(display, "pos_"+(pos+1)+".jpg");
		number.setRenderState(ts1);
		number.setLocalTranslation(position.getX()+ WIDHT/2+15, position.getY(), position.getZ());
		
		for(PointBoardScore p: scores){
			p.recalcPosition();
		}
	}

	
	private void drawScores() {
		Text t;
		for(PointBoardScore p: scores){
			t= p.getFirstText();
			t.updateRenderState();
			t.draw(display.getRenderer());
			
			t= p.getSecondText();
			t.updateRenderState();
			t.draw(display.getRenderer());
			
			t= p.getTotalText();
			t.updateRenderState();
			t.draw(display.getRenderer());
		}
	}


	public void addScore(PointBoardScore score){
		scores.add(score);		
	}

	public Vector3f getPosition(int id, int i) {
		int pos, cell, row=FIRST_ROW;
		
		if(id>5){
			id++;
			row=SECOND_ROW;
			id-=6;
		}
		
		if(i==3){
			i=1;
			row-= TOTAL_ROW;
		}
		
		
		switch (id) {
			case 1:
				pos=-3;
				cell=(6-i)*-1;
				break;
			case 2:
				pos=-2;
				cell=(4-i)*-1;
				break;
			case 3:
				pos=-1;
				cell= (2-i)*-1;
				break;
			case 4:
				pos=1;
				cell= (-1+i);
				break;
			case 5:
				pos=2;
				cell= (1+i);
				break;
			case 6:
				pos=3;
				cell= (3+i);
				break;
			default:
				pos=0;
				cell=0;
				break;
		}
		

		return new Vector3f(position.getX()+SPACE*pos+CELL*cell, position.getY()+row, position.getZ());
		
	}

	public PointBoardScore addNewPoint(int shootCounter, int point) {
		PointBoardScore p=new PointBoardScore(shootCounter, point, this);
		addScore(p);
		
		return p;
	}
	
}
